home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / COMPTOOL / ACTVCOMP / COFFEE / CO2CMON2.CLS < prev    next >
Encoding:
Visual Basic class definition  |  1996-11-27  |  7.5 KB  |  199 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CoffeeMonitor2"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = False
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Option Explicit
  11. ' > For an overview of this sample application, search
  12. '   online Help for Coffee.
  13. ' > AboutCof.Txt, in the Related Documents folder of
  14. '   CoffWat2.vbp, also contains information about the sample.
  15.  
  16. ' CoffeeMonitor2 class
  17. ' --------------------
  18. ' Like the CoffeeMonitor object, CoffeeMonitor2
  19. '   monitors an imaginary serial interface to a high-
  20. '   tech coffee pot, using a timer to determine
  21. '   how often to check the coffee status.
  22. '
  23. ' Instead of raising an event when the coffee's ready,
  24. '   the CoffeeMonitor2 object invokes a call-back method
  25. '   that must be implemented by one of the client's
  26. '   classes.  The call-back method is declared in the
  27. '   ICallBack class.
  28. '
  29. ' (Since the high-tech coffee pot has not yet been
  30. '   invented, this sample application simply invokes the
  31. '   call-back method every ten seconds.)
  32. '
  33. ' IMPORTANT: To simplify a rather complex example, the
  34. '   ICallBack class has been included in this project.
  35. '   This will NOT work in real-life systems, which usually
  36. '   go through many versions.  If a second version of
  37. '   Coffee2 is made, the interface ID version number will
  38. '   be incremented, and earlier clients will NOT work with
  39. '   the new version of Coffee2.  Standard interfaces like
  40. '   ICallBack should be created by themselves in small DLLs
  41. '   which can be referenced by both client and component.
  42. '   Once an interface is in use by finished applications,
  43. '   it must never be changed.  For more information, search
  44. '   for "polymorphism" in Books Online.
  45. '
  46. ' Note that the CoffeeMonitor2 class's Instancing property
  47. '   is set to PublicNotCreatable.  This means that clients
  48. '   cannot create a CoffeeMonitor2; they can only get a
  49. '   reference to the shared CoffeeMonitor2 by creating a
  50. '   Connector2 object and accessing its CoffeeMonitor2
  51. '   property.
  52. '
  53. ' Like the CoffeeMonitor class, the CoffeeMonitor2 class
  54. '   fixes the bug described in the topic "Using the Shared
  55. '   CoffeeMonitor," in "Creating an ActiveX Exe Component,"
  56. '   in Books Online, whereby multiple CoffeeMonitor objects
  57. '   could sometimes be created.
  58.  
  59. ' =======================================================
  60. '  WARNING!  Code-only timers are inherently dangerous
  61. '       in the Visual Basic development environment,
  62. '       becaue the system blindly calls back into your
  63. '       code until the timer is turned off with an API
  64. '       call.  It's safer to use Timer controls during
  65. '       most of the development process, and only switch
  66. '       to call-back timers at the very end.
  67. ' =======================================================
  68.  
  69. Const ICN_ARRAYINCREMENT = 10
  70.  
  71. ' maicnClients stores references to all the clients that have
  72. ' ------------   requested call-backs.  (Note that this is
  73. '   different from the use of events in CoffeeMonitor;
  74. '   one event can be received by any number of clients, while
  75. '   call-backs must be made one by one.)  An array is used,
  76. '   rather than a Collection, because Collection objects keep
  77. '   objects in Variants, resulting in late binding.
  78. Private maicnClients() As ICoffeeNotify
  79. Private mlngMaxClients As Long
  80.  
  81. ' mXTimer holds a reference to a code-only timer that
  82. ' -------   tells CoffeeMonitor2 when to check the pot.
  83. '   Because the variable is declared WithEvents, the
  84. '   CoffeeMonitor2 object receives the XTimer object's Tick
  85. '   events (see Sub mwXTimer_Tick, below).  Code for the
  86. '   XTimer object can be found in XTimers.vbp.
  87. Private WithEvents mwXTimer As XTimer
  88. Attribute mwXTimer.VB_VarHelpID = -1
  89.  
  90. Private Sub Class_Initialize()
  91.     ' Allocate some space in the array of client objects.
  92.     mlngMaxClients = ICN_ARRAYINCREMENT
  93.     ReDim maicnClients(1 To mlngMaxClients)
  94.     '
  95.     ' Create the XTimer object.  When this assignment is
  96.     '   made, Visual Basic connects the XTimer's Tick event
  97.     '   to the mwXTimer_Tick event procedure (see below).
  98.     Set mwXTimer = New XTimer
  99.     '
  100.     ' The timer is set to tick every ten seconds (10,000
  101.     '   milliseconds).
  102.     mwXTimer.Interval = 10000
  103.     mwXTimer.Enabled = True
  104. End Sub
  105.  
  106. Private Sub Class_Terminate()
  107.     Dim intCt As Integer
  108.     
  109.     ' It's important to disable the XTimer before releasing
  110.     '   it.  As described in XTimers.vbp, abandoning a
  111.     '   running XTimer essentially leaks a system timer
  112.     '   until XTimers.DLL finally shuts down.
  113.     mwXTimer.Enabled = False
  114.     Set mwXTimer = Nothing
  115.     '
  116.     ' Release all remaining call-back clients, in case they
  117.     '   released CoffeeMonitor2 without first requesting
  118.     '   an end to notifications.
  119.     For intCt = 1 To mlngMaxClients
  120.         Set maicnClients(intCt) = Nothing
  121.     Next
  122.     '
  123.     Debug.Print "CoffeeMonitor2 (call-backs) terminated at " & Now
  124. End Sub
  125.  
  126. ' TellMeReady is called by a client who wants to receive a
  127. ' -----------   call-back when the coffee is ready.  The
  128. '   client must implement the ICoffeeNotify interface,
  129. '   defined in the ICoffeeNotify class.
  130. '
  131. Public Sub TellMeReady(ByVal icn As ICoffeeNotify)
  132.     Dim lngCt As Long
  133.     
  134.     ' Find an opening in the array of interfaces.
  135.     For lngCt = 1 To mlngMaxClients
  136.         If maicnClients(lngCt) Is Nothing Then Exit For
  137.     Next
  138.     '
  139.     ' If there were no openings, grow the array.
  140.     If lngCt > mlngMaxClients Then
  141.         mlngMaxClients = mlngMaxClients + ICN_ARRAYINCREMENT
  142.         ReDim Preserve maicnClients(1 To mlngMaxClients)
  143.     End If
  144.     '
  145.     Set maicnClients(lngCt) = icn
  146.     '
  147.     ' Give the object the index of its entry, as a key for
  148.     '   quick lookup when disconnection is requested.
  149.     icn.NotifyID = lngCt
  150. End Sub
  151.  
  152. ' CeaseCallBacks removes the client from the list of objects
  153. ' --------------   receiving call-back notifications, using
  154. '   the key the object was assigned when it requested
  155. '   notifications.
  156. '
  157. Public Sub CeaseCallBacks(ByVal icn As ICoffeeNotify)
  158.     Set maicnClients(icn.NotifyID) = Nothing
  159. End Sub
  160.  
  161. ' mwXTimer_Tick is the event procedure CoffeeMonitor2 uses
  162. ' -------------   to receive the XTimer object's Tick
  163. '   events.  The name of an event procedure that's
  164. '   associated with a WithEvents variable always has the
  165. '   variable name as a prefix.
  166. '
  167. Private Sub mwXTimer_Tick()
  168.     Dim lngCt As Long
  169.     
  170.     ' (Code to test serial port omitted.)
  171.     '
  172.     On Error Resume Next
  173.     '
  174.     ' The call-back method must be called for each object
  175.     '   that has requested a notification.
  176.     For lngCt = 1 To mlngMaxClients
  177.         If Not maicnClients(lngCt) Is Nothing Then
  178.             maicnClients(lngCt).CoffeeReady
  179.             If Err.Number <> 0 Then
  180.                 ' Error &H80010005 is ignored, because it
  181.                 '   can be caused by the client object being
  182.                 '   temporarily unresponsive.
  183.                 If Err.Number <> &H80010005 Then
  184.                     ' If a client application has closed without
  185.                     '   ending the notifications, remove it from
  186.                     '   the list.
  187.                     Set maicnClients(lngCt) = Nothing
  188.                 End If
  189.                 '
  190.                 ' When On Error Resume Next is used, the
  191.                 '   error number must be cleared after each
  192.                 '   error.
  193.                 Err.Number = 0
  194.             End If
  195.         End If
  196.     Next
  197. End Sub
  198.  
  199.